package com.googlecode.lookie.platform.flexdock;

import org.flexdock.docking.DockingManager;
import org.flexdock.docking.drag.effects.DragPreview;
import org.flexdock.docking.drag.effects.EffectsManager;
import org.flexdock.perspective.PerspectiveFactory;
import org.flexdock.perspective.PerspectiveManager;
import org.flexdock.perspective.persist.FilePersistenceHandler;
import org.flexdock.perspective.persist.PersistenceHandler;
import org.flexdock.perspective.persist.xml.XMLPersister;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.richclient.application.ApplicationPage;
import org.springframework.richclient.application.ApplicationPageFactory;
import org.springframework.richclient.application.ApplicationWindow;
import org.springframework.richclient.application.PageDescriptor;
import org.springframework.richclient.settings.SettingsManager;

public class FlexDockApplicationPageFactory
    implements ApplicationPageFactory, InitializingBean
{

    private PerspectiveFactory perspectiveFactory;

    private String defaultPerspective;

    private DragPreview dragPreview;

    private boolean floatingEnabled;

    private boolean singleTabsAllowed;

    private Resource layoutResource;

    private SettingsManager settingsManager;

    protected void setSettingsManager( SettingsManager settingsManager )
    {
        this.settingsManager = settingsManager;
    }

    public PerspectiveFactory getPerspectiveFactory()
    {
        return perspectiveFactory;
    }

    public void setPerspectiveFactory( PerspectiveFactory perspectiveFactory )
    {
        this.perspectiveFactory = perspectiveFactory;
    }

    public String getDefaultPerspective()
    {
        return defaultPerspective;
    }

    public void setDefaultPerspective( String defaultPerspective )
    {
        this.defaultPerspective = defaultPerspective;
    }

    public DragPreview getDragPreview()
    {
        return dragPreview;
    }

    public void setDragPreview( DragPreview dragPreview )
    {
        this.dragPreview = dragPreview;
    }

    public boolean isFloatingEnabled()
    {
        return floatingEnabled;
    }

    public void setFloatingEnabled( boolean floatingEnabled )
    {
        this.floatingEnabled = floatingEnabled;
    }

    public boolean isSingleTabsAllowed()
    {
        return singleTabsAllowed;
    }

    public void setSingleTabsAllowed( boolean singleTabsAllowed )
    {
        this.singleTabsAllowed = singleTabsAllowed;
    }

    public Resource getLayoutResource()
    {
        return layoutResource;
    }

    public void setLayoutResource( Resource layoutResource )
    {
        this.layoutResource = layoutResource;
    }

    public ApplicationPage createApplicationPage( ApplicationWindow window, PageDescriptor descriptor )
    {
        FlexDockApplicationPage page = new FlexDockApplicationPage();

        page.setApplicationWindow( window );
        page.setDescriptor( descriptor );

        DockingManager.setDockableFactory( page );

        PerspectiveManager.getInstance().setCurrentPerspective( "explorer", true );

        page.loadLayout();

        return page;
    }

    public void afterPropertiesSet()
        throws Exception
    {
        if ( dragPreview != null )
        {
            EffectsManager.setPreview( dragPreview );
        }

        DockingManager.setFloatingEnabled( floatingEnabled );
        DockingManager.setSingleTabsAllowed( singleTabsAllowed );

        PerspectiveManager.setFactory( perspectiveFactory );

        PerspectiveManager mgr = PerspectiveManager.getInstance();
        mgr.setDefaultPerspective( defaultPerspective );

        if ( layoutResource != null )
        {
            PersistenceHandler persister = new FilePersistenceHandler( layoutResource.getFile(), XMLPersister
                .newDefaultInstance() );
            PerspectiveManager.setPersistenceHandler( persister );

            // remember to store on shutdown
            DockingManager.setAutoPersist( true );
        }
    }

}
